package com.linjian.togetherbirthday.fragment;

import android.os.Bundle;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.google.gson.Gson;
import com.linjian.togetherbirthday.R;
import com.linjian.togetherbirthday.activity.SplashActivity;
import com.linjian.togetherbirthday.base.BaseFragment;
import com.sdsmdg.tastytoast.TastyToast;

import java.util.HashMap;
import java.util.Map;

public class ChooseGenderFragment extends BaseFragment implements View.OnClickListener {

    private LinearLayout genderLL;
    private ImageView msIc, menIc;
    private Button nestBt;
    private SplashActivity splashActivity;

    private int setSex;

    public static ChooseGenderFragment newInstance() {
        ChooseGenderFragment chooseGenderFragment = new ChooseGenderFragment();
        Bundle bundle = new Bundle();
//        bundle.putInt(Integer.class.getName(), resId);
        chooseGenderFragment.setArguments(bundle);
        return chooseGenderFragment;
    }

    @Override
    protected int setView() {
        return R.layout.fragmnet_gender;
    }

    @Override
    protected void init(View view) {
        genderLL = view.findViewById(R.id.gender_ll);
        msIc = view.findViewById(R.id.gender_ms_ic);
        menIc = view.findViewById(R.id.gender_men_ic);
        nestBt = view.findViewById(R.id.bt_go_gender);
        splashActivity = (SplashActivity)getActivity();
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        setListener();
    }

    private void setListener() {
        msIc.setOnClickListener(this);
        menIc.setOnClickListener(this);
        nestBt.setOnClickListener(this);
    }

    /**
     * 缩放动画
     */
    public void Scale() {
        TranslateAnimation translateAnimation = (TranslateAnimation) AnimationUtils.loadAnimation(getActivity(), R.anim.gender_top);
        genderLL.startAnimation(translateAnimation);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.gender_ms_ic:
                setSex = 2;
                Scale();
                msIc.setImageResource(R.mipmap.ms_select);
                menIc.setImageResource(R.mipmap.men_normal);
                nestBt.setVisibility(View.VISIBLE);
                break;
            case R.id.gender_men_ic:
                setSex = 1;
                Scale();
                msIc.setImageResource(R.mipmap.ms_normal);
                menIc.setImageResource(R.mipmap.men_select);
                nestBt.setVisibility(View.VISIBLE);
                break;
            case R.id.bt_go_gender:
                if (setSex == 0){
                    TastyToast.makeText(getActivity(),"请先选择性别",TastyToast.LENGTH_SHORT,TastyToast.WARNING).show();
                }else {
                    Map<String,Object> map = new HashMap<>();
                    map.put("sex",setSex);
                    splashActivity.replaceFragment("BirthFragment",new Gson().toJson(map));
                }

                break;
        }
    }
}
